import { message } from 'antd';
// import _ from 'lodash';
import { getMembers, addMembere, updateMember, updateEmployeeStatus, deletEmployee } from '../../services/store';


export default {
    namespace: 'employee',
    state: {
        ns: 'employee',
        data: null,
        editorData: {},
        visible: false,
        pagination: {
            page: 1,
            pageSize: 10,
        },
    },
    subscriptions: {
        setup(args) {
            const { dispatch, history } = args;
            history.listen(({ pathname }) => {
                if (pathname === '/store/employee') {
                    dispatch({
                        type: 'init',
                    });
                }
            });
        }
    },
    effects: {
        * init(payload, { put }) {
            yield put({
                type: 'load',
            });
        },
        * load(payload, { put, call, select }) {
            const {
                pagination: { page, pageSize },
            } = yield select(({ employee }) => employee);
            const {
                data: pagination,
                data: { datas: data } = {},
                code,
                msg,
            } = yield call(getMembers, { page, pageSize});
            if (code === 1) {
                yield put({
                    type: 'upState',
                    payload: {
                        data,
                        pagination,
                    },
                });
            } else {
                message.error(msg);
            }
        },
        * changeStatus({ payload }, { call, put }) {
            console.log(payload);
            const { code, msg, err: e } = yield call(updateEmployeeStatus, payload);
            if (code === 1) {
                message.success(msg);
                yield [
                    put({
                        type: 'init',
                    }),
                ];
            } else {
                message.error(msg || e);
            }
        },
        * editorOk({ payload: { err, isNew, values } }, { call, put}) {
            if (err) {
                return;
            }
            let res;
            if (!isNew) {
                console.log(values);
                res = yield call(updateMember, values);
            } else {
                res = yield call(addMembere, values);
            }
            const { code, msg, err: e } = res;
            if (code === 1) {
                message.success(msg);
                yield [
                    put({
                        type: 'editorVisible',
                    }),
                    put({
                        type: 'load',
                    }),
                ];
            } else {
                message.error(msg || e);
            }
        },
        * removeData({ selectId }, { call, put, }) {
            const { code, msg, err } = yield call(deletEmployee, {empId: selectId});
            if (code === 1) {
                yield [
                    put({
                        type: 'init',
                    }),
                    call(message.success, msg)
                ];
            } else {
                message.error(msg, err);
            }
        },
    },
    reducers: {
        upState(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
        editorVisible(state, { payload: { editorData } = {} }) {
            console.log(editorData);
            if (editorData) {
                console.log(editorData);
                return {
                    ...state,
                    visible: !!editorData,
                    editorData,
                };
            } else {
                return {
                    ...state,
                    visible: false,
                };
            }
        },
    }
};
